package option;

import display.BodyPanel;
import display.GameFrame;
import main.StartGame;

import java.util.Random;

public class Sweeper {
    public static final int[][] directions = new int[][]{
            {0, -1, -1, -1, 0, 1, 1, 1},
            {-1, -1, 0, 1, 1, 1, 0, -1}
    };/*方向遍历数组*/
    public static int[][] sweeper;/*储存雷*/
    public static int[][] isVisited;/*雷是否点击*/
    public static int count = 0;/*点击次数*/

    /**
     * 重新开始新游戏
     */
    public static void newGame() {
        BodyPanel.time.ifFinished=true;
        initSweeper();
        GameFrame newGameFrame=new GameFrame();
        newGameFrame.setVisible(true);
        StartGame.gameframe.dispose();
        StartGame.gameframe=newGameFrame;
    }

    /**
     * 初始化雷的数据层
     */
    public static void initSweeper() {
        count=0;
        sweeper = new int[Data.x][Data.y];/*创建新的雷数组*/
        isVisited = new int[Data.x][Data.y];/*创建新的雷访问数组*/
        int sweepercount = 0;/*已安装雷数*/
        Random random = new Random();
        /*安装雷*/
        while (sweepercount < Data.leiCount) {
            int i = random.nextInt(Data.x);
            int j = random.nextInt(Data.y);
            if (sweeper[i][j] != -1) {
                sweeper[i][j] = -1;
                sweepercount++;
            }
        }
        /*设置雷周围的数字*/
        for (int i = 0; i < Data.x; i++) {
            for (int j = 0; j < Data.y; j++) {
                if (sweeper[i][j] == -1) continue;
                /*8个方向*/
                for (int k = 0; k < 8; k++) {
                    int x = i + directions[0][k];
                    int y = j + directions[1][k];
                    if (x >= 0 && x < Data.x && y >= 0 && y < Data.y && sweeper[x][y] == -1) sweeper[i][j]++;
                }
            }
        }
    }
}
